package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.daos.VisitorDAOImpl;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_visitorLogin.jsp");  
		rd.forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html");  
	    PrintWriter out = response.getWriter();
	    
	    HttpSession session=request.getSession(true);
	         
	    String n=request.getParameter("username");  
	    String p=request.getParameter("password");  
	          
	    Visitor u =new Visitor();
	    u.setVisitorName(n);
	    u.setPassword(p);
	    
	    VisitorService vs = new VisitorService();
	    
	    try
	    {

	    	
	    	if(vs.login(u)){
	    		
	    		u.setIdVisitor(vs.getIdVisitor(u));
	    		u=vs.getVisitor(u);

	    		session.setAttribute("visitorName",n);
	    		session.setAttribute("password",p);
	    		session.setAttribute("idVisitor", u.getIdVisitor());
	    		session.setAttribute("firstName",u.getFirstName());
	    		session.setAttribute("lastName",u.getLastName());
	    		session.setAttribute("email",u.getEmail());
	    		session.setAttribute("phone",u.getPhone());
	    		session.setAttribute("address", u.getAddress());
	    		session.setAttribute("dni", u.getDni());
	    		
	    		
	    		
	    		if(vs.isAdmin(u))
	    		{
	    			session.setAttribute("isAdmin",1);
	    			ViewEventsofUserServlet view=new ViewEventsofUserServlet();
	    			view.doPost(request, response);
	    			//RequestDispatcher rd=request.getRequestDispatcher("NewCodington_myPortalAdmin.jsp");  
	    			//rd.forward(request,response);
	    		}
	    		else{
	    			session.setAttribute("isAdmin",0);
	    			ViewEventsofUserServlet view=new ViewEventsofUserServlet();
	    			view.doPost(request, response);
	   	 			//RequestDispatcher rd=request.getRequestDispatcher("NewCodington_myPortal.jsp");  
	    			//rd.forward(request,response);  
	    		}
	    	}  
	    	else{  
	    		session.setAttribute("error", "Sorry, Visitor Name or Password incorrect");
	    		
	    		//out.print("Sorry username or password error");  
	    		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_visitorLogin.jsp");  
	    		rd.include(request,response);
	    	} 
	    }
	    catch(Exception eee){
	    	eee.getMessage();	
	    }
	          
	    out.close();  
	}

}
